﻿Imports System.Windows.Forms

Public Class frmImportSB19

    Private DatCnvProcess As Process = Nothing

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        SaveForm()
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnConfig_Click(sender As System.Object, e As System.EventArgs) Handles btnConfig.Click
        Try
            With New OpenFileDialog
                .AddExtension = True
                .CheckFileExists = True
                .CheckPathExists = True
                .DefaultExt = ".xmlcon"
                .FileName = txtConfig.Text
                .Filter = "SB-19 Configuration Files (*.xmlcon)|*.xmlcon"
                .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\WRDB"
                If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                    txtConfig.Text = .FileName
                End If
            End With
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub LoadForm()
        Try
            GetControlValue(REGAPP, txtConfig)
            Project.DB.FillPickList(cboStationID, "", "Stations", "Station_ID", "Station_Name", , GetSetting(REGAPP, Me.Name, cboStationID.Name))
            GetControlValue(REGAPP, txtDateTime)
            Project.DB.FillPickList(cboCCode, "", "CCodes", "CCode", "CCode_Name", "", GetSetting(REGAPP, Me.Name, cboCCode.Name))
            For i As Integer = 0 To 5
                Dim cbo As ComboBox = Choose(i + 1, cboTemp, cboCond, cboDO, cboPH, cboTurb, cboSalin)
                Project.DB.FillPickList(cbo, "", "PCodes", "PCode", "Anal_Name", "(Ignore)", GetSetting(REGAPP, Me.Name, cbo.Name))
            Next
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub SaveForm()
        Try
            SaveControlValue(REGAPP, txtConfig)
            SaveSetting(REGAPP, Me.Name, cboStationID.Name, Project.DB.GetPickList(cboStationID))
            SaveControlValue(REGAPP, txtDateTime)
            SaveSetting(REGAPP, Me.Name, cboCCode.Name, Project.DB.GetPickList(cboCCode))
            For i As Integer = 0 To 5
                Dim cbo As ComboBox = Choose(i + 1, cboTemp, cboCond, cboDO, cboPH, cboTurb, cboSalin)
                SaveSetting(REGAPP, Me.Name, cbo.Name, Project.DB.GetPickList(cbo))
            Next
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub frmImportSB19_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Try
            If DatCnvProcess IsNot Nothing AndAlso Not DatCnvProcess.HasExited Then DatCnvProcess.Kill()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub frmImportSB19_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        LoadForm()
    End Sub

    Private Sub txtDateTime_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtDateTime.Validating
        Try
            Dim dt As Date = Nothing
            If Not DateTime.TryParse(txtDateTime.Text, dt) Then
                WarningMsg("The date-time you entered is invalid; please use format appropriate to your current culture settings.")
                e.Cancel = True
            End If
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnConfigure_Click(sender As System.Object, e As System.EventArgs) Handles btnConfigure.Click
        DatCnvProcess = Process.Start("datcnvw.exe")
    End Sub
End Class
